package com.cman777.springc.springali.sample.provider.client.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.cman777.springc.common.bean.exception.BusinessException;
import com.cman777.springc.springali.sample.provider.bean.po.User;
import com.cman777.springc.springali.sample.provider.client.IClientA;
import com.cman777.springc.springali.sample.provider.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author chenzhicong
 * @time 2020/12/1 17:16
 */
@Service
public class ClientA implements IClientA {
    @Autowired
    private UserService userService;
    @Override
    public String greet(String name) {
        return "hello, " + name;
    }

    @Override
    public void Step1() {
        User user  = new User();
        user.setAge(1);
        userService.save(user);
    }

    @Override
    public void Step2() {
        throw new BusinessException("异常");
     /*   User user  = new User();
        user.setAge(2);
        userService.save(user);*/

    }
    /**
      *  blockHandler 为限流降级的处理逻辑
      *  fallback用于在抛出异常的时候提供 fallback 处理逻辑（除了 exceptionsToIgnore 里面排除掉的）
      *  若 blockHandler 和 fallback 都进行了配置，
      *  则被限流降级而抛出 BlockException 时只会进入 blockHandler 处理逻辑。
      *  若未配置 blockHandler、fallback 和 defaultFallback，
      *  则被限流降级时会将 BlockException 直接抛出（若方法本身未定义 throws BlockException
      *  则会被 JVM 包装一层 UndeclaredThrowableException）
      */
    @Override
    @SentinelResource(blockHandler = "testSentinelBlock",fallback = "testSentinelFallback")
    public String testSentinel() {
        throw new BusinessException("异常");
    }

    public String testSentinelBlock(BlockException ex){
            ex.printStackTrace();
            return "限流降级";
    }

    public String testSentinelFallback(){
        return "异常降级";
    }
}
